<?php

include('../ZmopSdk.php');
include('./ZmopClient.php');
include('./request/ZhimaAuthInfoAuthorizeRequest.php');  

class ZhimaAuthInfoAuthorize {
    //芝麻信用网关地址
    public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
    //商户私钥文件
    public $privateKeyFile = "./cert/rsa_private_key.pem";
    //芝麻公钥文件
    public $zmPublicKeyFile = "./cert/zima_public_key.pem";
    //数据编码格式
    public $charset = "UTF-8";
    //芝麻分配给商户的 appId
    public $appId = "1000791";
    
    public function testZhimaAuthInfoAuthorize(){
         $client = new ZmopClient($this->gatewayUrl,$this->appId,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);
         $request = new ZhimaAuthInfoAuthorizeRequest();
         $request->setChannel("app");
         $request->setPlatform("zmop");
                $request->setIdentityType("2");// 必要参数         
                $request->setIdentityParam("{\"name\":\"李黄德\",\"certType\":\"IDENTITY_CARD\",\"certNo\":\"533523198005010019\"}");// 必要参数         
                $request->setBizParams("{\"auth_code\":\"M_H5\",\"channelType\":\"app\",\"state\":\"商户自定义\"}");// 
                  $url = $client->generatePageRedirectInvokeUrl($request);
                  //echo $url;
          //echo json_encode($response);
    }
    //获取芝麻分
    public function ZhimaCreditScoreGet(){
        $client = new ZmopClient($this->gatewayUrl,$this->appId,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);
        $request = new ZhimaCreditScoreGetRequest();
        $request->setChannel("apppc");
        $request->setPlatform("zmop");
        $tid = date('YmdHis').mt_rand(100,999).time().mt_rand(100,999);
        $request->setTransactionId($tid);//必要参数，业务流水号
        $request->setProductCode("w1010100100000000001");//必要参数，这个值对于芝麻分产品是固定的，无需修改
        $request->setOpenId("268803904375259775763039660");//必要参数，授权获得的openid
        //print_r($request);exit;
        $response = $client->execute($request);
        echo json_encode($response);
        //{"success":true,"biz_no":"ZM201610263000000556600308114916","zm_score":"688"}
    }
    //解密函数
    public  function zhimacallback($params,$sign){
        //$this->privateKeyFile= "./cert/rsa_private_key.pem";
        $client = new ZmopClient($this->gatewayUrl, $this->appId, $this->charset, $this->privateKeyFile,$this->zmPublicKeyFile);
        $result=$client->decryptAndVerifySign($params,$sign);
        echo $result;
        //open_id=268803904375259775763039660&error_message=%E6%93%8D%E4%BD%9C%E6%88%90%E5%8A%9F&state=%E5%95%86%E6%88%B7%E8%87%AA%E5%AE%9A%E4%B9%89&error_code=SUCCESS&app_id=1000791&success=true
        parse_str($result,$result1);

        print_r($result1);
        return $result;
    }
}
//$zm = new TestZhimaAuthInfoAuthorize();
//$zm->testZhimaAuthInfoAuthorize();

$params = 'UbnqqjQNAE4naUuP3UwtUw0c2o0gWBFcAW5a7jxu0SNPv73gZhTfa4UFuYCHsyfu7X%2FtyzFLDULFoz376eXbshJfjvsnU%2FHrZMImhD25A0w8KvJC3Geih%2FxZ8aAF%2F4uD9Ycywm1%2BGB9oAOmJz2ofYxUQdScMDOc0w7X5kcNBRnk57zpV0BxII8cr%2BDUajN600iLentBgc5z39BLd6v2RipB%2Bf7MlTdslWRygQWvXGvLZHHRgz8EK0IXBlNvn4NL3rEIwMPxb3U0tv7hxo2dNALXLXhIeYU0HKTjImIN6kA4QctZeZU73K%2BMDNnW595sbDRjPLp7cI6BQfzCNmHEQdA%3D%3D';
$sign = 'egfz8XfF%2FPfbCCxMz13MFe1mI%2Fi75bwwwBPQGLLO%2BJOnssIACzlfWfaLhobI7xC8%2FLr8Pc7kTRWX3Sr%2BAcNMgEeNsY4zhwk4ZkfeCW9e4lRYk4kE%2FlgCiecwbAJ2MxA1Ola4JvlPw4m9S0L0qqCoRHZKqdWSmuUD4zw0GALbLFM%3D';
$zm = new ZhimaAuthInfoAuthorize();
//$zm->zhimacallback(urldecode($params),urldecode($sign));
//$zm->ZhimaCreditScoreGet();
//open_id=268803904375259775763039660&error_message=%E6%93%8D%E4%BD%9C%E6%88%90%E5%8A%9F&state=%E5%95%86%E6%88%B7%E8%87%AA%E5%AE%9A%E4%B9%89&error_code=SUCCESS&app_id=1000791&success=true

/*
http://api.9maibei.com/Home/Zhima/notifyUrl?params=UbnqqjQNAE4naUuP3UwtUw0c2o0gWBFcAW5a7jxu0SNPv73gZhTfa4UFuYCHsyfu7X%2FtyzFLDULFoz376eXbshJfjvsnU%2FHrZMImhD25A0w8KvJC3Geih%2FxZ8aAF%2F4uD9Ycywm1%2BGB9oAOmJz2ofYxUQdScMDOc0w7X5kcNBRnk57zpV0BxII8cr%2BDUajN600iLentBgc5z39BLd6v2RipB%2Bf7MlTdslWRygQWvXGvLZHHRgz8EK0IXBlNvn4NL3rEIwMPxb3U0tv7hxo2dNALXLXhIeYU0HKTjImIN6kA4QctZeZU73K%2BMDNnW595sbDRjPLp7cI6BQfzCNmHEQdA%3D%3D&sign=egfz8XfF%2FPfbCCxMz13MFe1mI%2Fi75bwwwBPQGLLO%2BJOnssIACzlfWfaLhobI7xC8%2FLr8Pc7kTRWX3Sr%2BAcNMgEeNsY4zhwk4ZkfeCW9e4lRYk4kE%2FlgCiecwbAJ2MxA1Ola4JvlPw4m9S0L0qqCoRHZKqdWSmuUD4zw0GALbLFM%3D
*/
/*
include('../ZmopSdk.php');
include('./ZmopClient.php');
include('./request/ZhimaAuthInfoAuthorizeRequest.php');
class TestAuthFreeze {
        //芝麻信用网关地址
        public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
        //商户公钥文件
        //芝麻公钥文件
        public $privateKeyFile = "cert/rsa_private_key.pem";
        public $zmPublicKeyFile = "cert/rsa_public_key.pem";

        //数据编码格式
        public $charset = "UTF-8";
        //芝麻分配给商户的appId
        public $appId = "1000791";


    //生成移动端SDK 集成需要的sign 参数 ，并进行urlEncode
    public function generateSign($certNo,$name,$certType='IDENTITY_CARD'){
        $client = new ZmopClient($this->gatewayUrl, $this->appId, $this->charset, $this->privateKeyFile,$this->zmPublicKeyFile);
 
        $request = new ZhimaAuthInfoAuthorizeRequest();
        $request->setScene("test");
        // 授权来源渠道设置为appsdk
        $request->setChannel("appsdk");
        // 授权类型设置为2标识为证件号授权见“章节4中的业务入参说明identity_type”
        $request->setIdentityType("2");
        // 构造授权业务入参证件号，姓名，证件类型;“章节4中的业务入参说明identity_param”
        $request->setIdentityParam("{\"certNo\":\"$certNo\",\"certType\":\"IDENTITY_CARD\", \"name\":\"$name\"}");
        // 构造业务入参扩展参数“章节4中的业务入参说明biz_params”
        $request->setBizParams("{\"auth_code\":\"M_APPSDK\"}");

        $params = $client->generateEncryptedParamWithUrlEncode($request);
        $sign = $client->generateSignWithUrlEncode($request);

        $data['gatewayUrl'] = $this->gatewayUrl;
        $data['appId'] = $this->appId;
        $data['charset'] = $this->charset;
        $data['params']=$params;
        $data['sign'] = $sign;
        return $data;
    }

    // 解密
    public function zhimacallback($params){
        $this->privateKeyFile= "cert/rsa_private_key.pem";
        $client = new ZmopClient($this->gatewayUrl, $this->appId, $this->charset, $this->privateKeyFile,$this->zmPublicKeyFile);
        $result=$client->generateSignCallBack($params,$this->privateKeyFile);
        return $result;
    }
} 
$zm = new TestAuthFreeze();
print_r($zm->generateSign('123','22222'));
//$zm->zhimacallback();

*/
